/*
 * LocalLogger.java
 *
 * Created on April 4, 2007, 6:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package vendingMachine.localLogger;

import commpackage.*;
import java.util.Date;
import java.util.ListIterator;
import vendingMachine.finance.AuthorizeObject;
import vendingMachine.gui.ShoppingCart;
import salesDB.*;
import vendingMachine.gui.ShoppingCartItem;

/**
 *
 * @author Anne Harmeson
 */
public class LocalLogger
{
    // DB
    private NoRacleSales salesDB;
    
    //Comm variables
    private ConnectionType type;
    private LocalLoggerCB cb;
    private CommFactory factory;
    private int sessionId;
    private Comm com;
    
    /** Creates a new instance of LocalLogger */
    public LocalLogger ()
    {
        System.out.println ("Creating Local Logger");
        
        salesDB = new NoRacleSales ();
        
        // set up client
        type = new ConnectionType ();
        type.setHostname ("localhost");
        type.setPort (3000);
        type.setSecurity (false);
        factory = new CommFactory ();
        cb = new LocalLoggerCB ();
        
        System.out.println ("Getting Comm Instance.");
        com = (Comm)factory.getCommInstance ();
        
        System.out.println ("Starting new Session.");
        sessionId = com.newSession (cb,type);
        
        com.connect (sessionId);
    }
    
    public void testConnection (String s)
    {
        System.out.println ("Transfering Test Sting to local site...");
        com.queueMessage (sessionId, s);
    }
    
    private boolean passToLocalSite (SalesRecord rec)
    {
        ///client like actions here...
        com.queueMessage (sessionId, rec);
        return true;
    }
    
    // Records the SalesRecord in the local DB and then
    // send the SalesRecord over the secure comm link to the Local Site.
    // If transmission was successful, the local DB record is deleted.
    public void processSale (SalesRecord rec)
    {
        ///unionize sale and transtion
        salesDB.addRecord (rec);
        
        System.out.println ("Transfering sale record to local site...");
        boolean status = passToLocalSite (rec);
        if(status)
        {
            salesDB.deleteRecord (rec.date, rec.transactionID);
        }
    }
    
    public void processSale (ShoppingCart cart, AuthorizeObject authO)
    {
        SalesRecord rec = new SalesRecord ();
        
        if ( cart == null )
        {
            System.out.println ("Error, cart objects null");
            return;
        }
        
        if ( authO != null)
        {
            rec.accountID = authO.getAccountId ();
            rec.authorizationID = authO.getAuthorizeId ();
        } else {
            System.out.println("Error, authO objects null");
        }
        
        rec.date = new Date ();
        
        rec.itemList = new LineItem[cart.getNumberOfItems ()];
        ListIterator it = cart.getItemList ().listIterator ();
        int i = 0;
        
        while( it.hasNext () )
        {
            LineItem item = new LineItem ();
            ShoppingCartItem scItem = (ShoppingCartItem)it.next ();
            if ( scItem.getItem () != null )
            {
                System.out.println (scItem.getItem ().getPrice ());                
                item.description = scItem.getItem ().getDescription ();
                item.itemID = scItem.getItem ().getID ();
                item.pricePer = scItem.getItem ().getPrice ();
            }
            System.out.println (scItem.getQuantity ());
            item.quantity = scItem.getQuantity ();
            rec.itemList[i] = item;
            i++;
        }
        
        ///rec.paymentMethod = SalesRecord.CREDIT;
        rec.transactionID = rec.date.hashCode ();
        rec.transactionTotal = cart.calculateTotal ();
        
        processSale (rec);
    }
}
